//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.ScientificWorkflow.UIDesigner
{
    #region NameSpace
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using Microsoft.Research.ScientificWorkflow.TridentUtilities;
    using SR = Microsoft.Research.DataLayer;
    using Microsoft.Research.ScientificWorkflow.TridentModel;
    using Microsoft.Research.ScientificWorkflow.TridentComposer;
    #endregion

    /// <summary>
    /// Interaction logic for WorkFlowMetaPopup.xaml
    /// </summary>
    public partial class WorkflowMetaPopup : UserControl
    {
        /// <summary>
        /// Fired on click of Ok button.
        /// </summary>
        public event EventHandler<InfoEnteredEventArgs> InfoEntered;
        
        /// <summary>
        /// Occurs when header drag.
        /// </summary>
        public event EventHandler HeaderDrag;
        
        /// <summary>
        /// In case of the category picker not being needed.
        /// </summary>
        public WorkflowMetaPopup()
        {
            InitializeComponent();
            this.brdrHeader.MouseLeftButtonDown += (sender, e) =>
            {
                if (this.HeaderDrag != null)
                    this.HeaderDrag.Invoke(sender, e);
            };

            this.txtWorkFlowName.Text = string.Empty;
            this.btnOk.IsEnabled = false;
            this.btnOk.Opacity = 0.7;
            this.KeyDown += new KeyEventHandler(WorkFlowMetaPopup_KeyDown);
            this.txtWorkFlowName.TextChanged += new TextChangedEventHandler(this.OnTextChanged);
            this.txtWorkFlowName.Focus();
        }

        /// <summary>
        /// Gets or sets the text that is displayed as the main header in this pop up.
        /// </summary>
        public string MainHeader
        {
            get
            {
                return this.txtBlkMainHeader.Text;
            }
            set
            {
                this.txtBlkMainHeader.Text = value;
            }
        }

        /// <summary>
        /// Gets or sets the text that is displayed to the user, that prompts on what is to be entered.
        /// </summary>
        public string WorkflowName
        {
            get
            {
                return this.txtWorkFlowName.Text;
            }
            set
            {
                this.txtWorkFlowName.Text = value;
                if (!string.IsNullOrEmpty(this.txtWorkFlowName.Text))
                {
                    this.txtWorkFlowName.SelectAll();
                }
            }
        }

        /// <summary>
        /// Gets or sets the text that is displayed to the user, that prompts on what is to be entered.
        /// </summary>
        public string WorkflowDescription
        {
            get
            {
                return this.txtWorkFlowDescription.Text;
            }
            set
            {
                this.txtWorkFlowDescription.Text = value;
                if (!string.IsNullOrEmpty(this.txtWorkFlowDescription.Text))
                {
                    this.txtWorkFlowDescription.SelectAll();
                }
            }
        }

        /// <summary>
        /// Set the default info to be shown to the user for input.
        /// </summary>
        public string CurrentInfo
        {
            get
            {
                return this.txtWorkFlowName.Text;                
            }

            set
            {
                this.txtWorkFlowName.Text = value;
                this.txtWorkFlowName.CaretIndex = this.txtWorkFlowName.Text.Length;
            }
        }

        /// <summary>
        /// Fired on text changed of the text box. Used to determine if the OK button should be enabled or not.
        /// </summary>
        /// <param name="sender">
        /// Name text box.
        /// </param>
        /// <param name="e">
        /// Text Changed EventArgs.
        /// </param>
        private void OnTextChanged(object sender, TextChangedEventArgs e)
        {
            if (this.OkButtonValidity())
            {
                this.EnableOkButton();
            }
            else
            {
                this.DisableOkButton();
            }
        }

        /// <summary>
        /// Enable the Ok button
        /// </summary>
        private void EnableOkButton()
        {
            this.btnOk.IsEnabled = true;
            this.btnOk.Opacity = 1.0;
        }

        /// <summary>
        /// Disable the OK button.
        /// </summary>
        private void DisableOkButton()
        {
            this.btnOk.IsEnabled = false;
            this.btnOk.Opacity = 0.7;
        }

        /// <summary>
        /// Fired on click of OK.
        /// </summary>
        /// <param name="sender">
        /// OK button.
        /// </param>
        /// <param name="e">
        /// RoutedEventArgs.
        /// </param>
        private void OnOkClick(object sender, RoutedEventArgs e)
        {
 
            if (this.InfoEntered != null)
            {
                Guid selectedGuid = Guid.Empty;
             
                InfoEnteredEventArgs args = new InfoEnteredEventArgs(this.txtWorkFlowName.Text, 
                    this.txtWorkFlowDescription.Text,
                    selectedGuid,
                    false);

                this.InfoEntered.Invoke(this, args);
            }
        }

        /// <summary>
        /// Fired on click of Cancel.
        /// </summary>
        /// <param name="sender">
        /// Cancel button.
        /// </param>
        /// <param name="e">
        /// RoutedEventArgs.
        /// </param>
        private void OnCancelClick(object sender, RoutedEventArgs e)
        {
            if (this.InfoEntered != null)
            {
                InfoEnteredEventArgs args = new InfoEnteredEventArgs(this.txtWorkFlowName.Text, 
                    this.txtWorkFlowDescription.Text,
                    Guid.Empty,
                    true);

                this.InfoEntered.Invoke(this, args);
            }
        }

        /// <summary>
        /// Handles the click of the close button.
        /// </summary>
        /// <param name="sender">
        /// Close button.
        /// </param>
        /// <param name="e">
        /// RoutedEventArgs.
        /// </param>
        private void OnCloseClick(object sender, RoutedEventArgs e)
        {
            this.OnCancelClick(this, e);
        }

        /// <summary>
        /// Check if the Ok button is to be enabled.
        /// </summary>
        /// <returns></returns>
        private bool OkButtonValidity()
        {
            // Check if name is entered and category is selected.
            if (!string.IsNullOrEmpty(this.txtWorkFlowName.Text.Trim()))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        
        /// <summary>
        /// Handles the Ok button click on the popup.
        /// </summary>
        /// <param name="sender">Source.</param>
        /// <param name="e">Event args.</param>
        private void WorkFlowMetaPopup_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                if (this.btnOk.IsEnabled)
                {
                    this.OnOkClick(this.btnOk, null);
                }
            }
        }
    }
}

